package com.ruibang.glass.equipment.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.DictEntityInfo;
import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.DeviceChildReq;
import com.ruibang.glass.equipment.domain.req.device.DeviceInfoReq;
import com.ruibang.glass.equipment.entity.device.DeviceChild;
import com.ruibang.glass.equipment.entity.device.EquipmentDeviceInfo;
import com.ruibang.glass.equipment.entity.device.ScrapReport;
import com.ruibang.glass.equipment.mapper.device.DeviceChildMapper;
import com.ruibang.glass.equipment.service.AreaService;
import com.ruibang.glass.equipment.service.DeviceChildService;
import com.ruibang.glass.equipment.service.EquipmentDeviceInfoService;
import com.ruibang.glass.equipment.service.WorkshopService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.tools.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 专设备管理服务
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/device/child")
@Api(value = "设备管理", tags = {"设备管理"})
public class DeviceController {

    @Autowired
    private DeviceChildMapper gDeviceChildMapper;

    @Autowired
    private DeviceChildService deviceChildService;

    @Autowired
    private EquipmentDeviceInfoService equipmentDeviceInfoService;

    @Autowired
    private AreaService areaService;
    @Autowired
    private WorkshopService workshopService;

    @Autowired
    private RedisSingleService redisService;




    @PostMapping("/getChildList")
    @ApiOperation(value = "获取子设备集合", response = ResultBody.class)
    public ResultBody getChildList(@RequestBody DeviceChild deviceChild) {
        return ResultBody.success(deviceChildService.getChildList(deviceChild));
    }

    @PostMapping("/getNewChildList")
    @ApiOperation(value = "获取子设备集合", response = ResultBody.class)
    public ResultBody getNewChildList(@RequestBody DeviceChildReq childReq) {
        return ResultBody.success(deviceChildService.getNewChildList(childReq));
    }

    @GetMapping("/getFirstChild")
    @ApiOperation(value = "获取子设备集合", response = ResultBody.class)
    public ResultBody getNewChildList() {
        List<DeviceChild> list = deviceChildService.list(Wrappers.<DeviceChild>lambdaQuery().select(DeviceChild::getId, DeviceChild::getChildName).eq(DeviceChild::getLevel, Constants.ONE_STR));
        return ResultBody.success(list);
    }


    @PostMapping("/addChild")
    @ApiOperation(value = "新增子设备", response = ResultBody.class)
    public ResultBody addChild(@RequestBody DeviceChild deviceChild) {
        deviceChildService.addDeviceChild(deviceChild);
        return ResultBody.success();
    }


    @PostMapping("/updateChild")
    @ApiOperation(value = "跟新子设备", response = ResultBody.class)
    public ResultBody updateChild(@RequestBody DeviceChild deviceChild) {
        gDeviceChildMapper.updateById(deviceChild);
        return ResultBody.success();
    }

    @GetMapping("/deleteChild/{id}")
    @ApiOperation(value = "删除子设备", response = ResultBody.class)
    public ResultBody deleteChild(@PathVariable("id") String id) {
        List<DeviceChild> deviceChildList = gDeviceChildMapper.selectList(Wrappers.<DeviceChild>lambdaQuery().eq(DeviceChild::getParentId, id));
        if (CollectionUtils.isEmpty(deviceChildList)) {
            deviceChildService.lambdaUpdate().set(DeviceChild::getIsDelete, Constants.ONE_STR).eq(DeviceChild::getId, id).update();
            return ResultBody.success();
        }
        return ResultBody.fail(300, "删除层级有子层级！暂时无法删除");
    }

    @PostMapping("/getPage")
    @ApiOperation(value = "获取设备管理列表", response = ResultBody.class)
    public ResultBody add(@RequestBody DeviceInfoReq deviceInfoReq) {
        return ResultBody.success(equipmentDeviceInfoService.getPage(deviceInfoReq));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增设备", response = ResultBody.class)
    public ResultBody addEquipmentDeviceInfo(@RequestBody EquipmentDeviceInfo equipmentDeviceInfo) {
        equipmentDeviceInfoService.addEquipmentDeviceInfo(equipmentDeviceInfo);
        return ResultBody.success();
    }

    @PostMapping("/removeByIds")
    @ApiOperation(value = "取消时删除所有子设备", response = ResultBody.class)
    public ResultBody removeByIds(@RequestBody EquipmentDeviceInfo equipmentDeviceInfo) {
        deviceChildService.lambdaUpdate().set(DeviceChild::getIsDelete, Constants.ONE_STR).in(DeviceChild::getId, equipmentDeviceInfo.getChildIdList()).update();
        return ResultBody.success();
    }

    @GetMapping("/getOne/{id}")
    @ApiOperation(value = "查询实体", response = ResultBody.class)
    public ResultBody getOne(@PathVariable("id") String id) {
        return ResultBody.success(equipmentDeviceInfoService.getById(id));
    }

    @GetMapping("/getAreaList/{id}")
    @ApiOperation(value = "获取工序", response = ResultBody.class)
    public ResultBody getAreaList(@PathVariable("id") String id) {
        return ResultBody.success(workshopService.getByProductLineAreaId(id));
    }

    @GetMapping("/getNewAreaList")
    @ApiOperation(value = "获取工序", response = ResultBody.class)
    public ResultBody getNewAreaList() {
        return ResultBody.success(areaService.getNewAreaList());
    }


    @PostMapping("/update/device")
    @ApiOperation(value = "修改设备", response = ResultBody.class)
    public ResultBody updateDevice(@RequestBody EquipmentDeviceInfo equipmentDeviceInfo) {
        equipmentDeviceInfoService.updateById(equipmentDeviceInfo);
        return ResultBody.success();
    }


    @PostMapping("/removeById")
    @ApiOperation(value = "删除设备管理数据", response = ResultBody.class)
    public ResultBody getAreaList(@RequestBody EquipmentDeviceInfo equipmentDeviceInfo) {
        equipmentDeviceInfoService.removeAll(equipmentDeviceInfo);
        return ResultBody.success();
    }


    @GetMapping("/getSelectList")
    @ApiOperation(value = "获取设备下拉", response = ResultBody.class)
    public ResultBody getSelectList() {
        List<EquipmentDeviceInfo> list = equipmentDeviceInfoService.list(
                Wrappers.<EquipmentDeviceInfo>lambdaQuery().select(EquipmentDeviceInfo::getId,
                        EquipmentDeviceInfo::getDeviceName, EquipmentDeviceInfo::getDeviceCode));
        return ResultBody.success(list);
    }


    @GetMapping("/getDictList")
    @ApiOperation(value = "获取字典下拉", response = ResultBody.class)
    public ResultBody getDictList() {
        List<DictEntityInfo> list = JsonUtils.parseArray((String) redisService.get(RedisKey.DICT_NEW_GROUP_TYPE), DictEntityInfo.class);
        return ResultBody.success(list);
    }
}
